/**
 * 
 */
package com.dalonedrau.jogl.opengl;

import java.awt.Dimension;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;

import com.dalonedrau.pool.PooledStringBuilder;
import com.dalonedrau.pool.StringBuilderPool;

/**
 * @author Donald
 */
public final class FontProperties {
	/** the one and only instance of the <code>SpriteLibrary</code> class. */
	private static FontProperties	instance;
	/**
	 * Gives access to the singleton instance of {@link FontProperties}.
	 * @return {@link FontProperties}
	 */
	public static FontProperties getInstance() {
		if (FontProperties.instance == null) {
			FontProperties.instance = new FontProperties();
		}
		return FontProperties.instance;
	}
	/**
	 * Initializes the UI.
	 * @param propertyFile the name of the properties file
	 * @throws Exception if there is an error loading values
	 */
	public static void init(final String propertyFile) throws Exception {
		BufferedReader input = null;
		InputStream is =
				FontProperties.class.getClassLoader().getResourceAsStream(
						propertyFile);
		if (is == null) {
			throw new Exception("Could not load stream");
		}
		input = new BufferedReader(new InputStreamReader(is));
		String inText = input.readLine(); // priming read
		// as long as lines are being read from file
		while (inText != null) {
			if (inText.length() != 0
					&& !inText.startsWith("#")) {
				String[] split = inText.split("=");
				FontProperties.setProperty(split[0], split[1]);
			}
			inText = input.readLine(); // read next line
		}
		input.close(); // not necessary, but still good practice
	}
	/**
	 * Sets a UI property.
	 * @param property the UI property
	 * @param value the UI property's value
	 * @throws Exception if there is an error setting the property
	 */
	private static void setProperty(final String property,
			final String value) throws Exception {
		PooledStringBuilder methodName =
				StringBuilderPool.getInstance().getStringBuilder();
		methodName.append("set");
		methodName.append(Character.toUpperCase(property.charAt(0)));
		methodName.append(property.substring(1));
		if (property.toLowerCase().indexOf("font") != -1) {
			// SampleInitUI.initializeFontProperty(value,
			// methodName.toString());
		}
		methodName.returnToPool();
	}
	/** the map of all defined font properties. */
	private HashMap<String, HashMap<Character, Dimension>>	fontProperties	=
		new HashMap<String, HashMap<Character, Dimension>>();
	/**
	 * Hidden constructor.
	 */
	private FontProperties() {}
	public HashMap<Character, Dimension> getCharacterMap(final String fontName,
			final int fontSize) {
		PooledStringBuilder sb =
				StringBuilderPool.getInstance().getStringBuilder();
		sb.append(fontName);
		sb.append("_");
		sb.append(fontSize);
		HashMap<Character, Dimension> map = fontProperties.get(sb.toString());
		StringBuilderPool.getInstance().returnObject(sb);
		return map;
	}
	/**
	 * Initializes the UI.
	 * @param propertyFile the name of the properties file
	 * @throws Exception if there is an error loading values
	 */
	public void initFontProperties(final String propertyFile)
			throws Exception {
		BufferedReader input = null;
		InputStream is =
				FontProperties.class.getClassLoader().getResourceAsStream(
						propertyFile);
		if (is == null) {
			throw new Exception("Could not load file as stream: " 
					+ propertyFile);
		}
		input = new BufferedReader(new InputStreamReader(is));
		String inText = input.readLine(); // priming read
		// as long as lines are being read from file
		HashMap<Character, Dimension> map = new HashMap<Character, Dimension>();
		while (inText != null) {
			if (inText.length() != 0) {
				char c = inText.charAt(0);
				Dimension d = new Dimension(
						Integer.parseInt(inText.substring(2).split(",")[0]),
						Integer.parseInt(inText.substring(2).split(",")[1])
						);
				map.put(c, d);
			}
			inText = input.readLine(); // read next line
		}
		input.close(); // not necessary, but still good practice
		if (!map.isEmpty()) {
			FontProperties.getInstance().fontProperties.put(
					propertyFile.split(".properties")[0],
					map
					);
		}
	}
}
